<template>
  <div class="goldPackageManagement">
    <h3>钻石套餐管理</h3>
    <div class="opration">
      <el-button type="success" icon="el-icon-plus" @click="addData" v-if="permissions_obj.id_144||permissions_obj.id_all">添加</el-button>
    </div>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="coins" label="钻石" width="90"></el-table-column>
        <el-table-column header-align="center" align="center" prop="send_type_text" label="赠送类型" width="90"></el-table-column>
        <el-table-column header-align="center" align="center" prop="send_num" label="赠送数量" width="90"></el-table-column>
        <el-table-column header-align="center" align="center" prop="price" label="价格" width="90"></el-table-column>
        <el-table-column header-align="center" align="center" prop="remark" label="标题描述" ></el-table-column>
        <el-table-column header-align="center" align="center" prop="intro" label="价格描述" ></el-table-column>
        <el-table-column header-align="center" align="center" prop="status_text" label="状态" width="70"></el-table-column>
        <el-table-column header-align="center" align="center" prop="sort" label="排序" width="60"></el-table-column>
         <el-table-column header-align="center" align="center" prop="created_at" label="创建时间" width="180"></el-table-column>
        <el-table-column header-align="center" align="center" prop="updated_at" label="编辑时间" width="180"></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)" v-if="permissions_obj.id_145||permissions_obj.id_all">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
    <!-- //新增修改弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="modifyEditorDialogVisible" width="40%" center>
      <div class="row">
        <span>钻石：</span>
        <el-input placeholder="请输入钻石数量" v-model="inputAmount" clearable></el-input>
      </div>
      <div class="row">
        <span>赠送类型：</span>
        <el-select v-model="inputGiveAway" placeholder="请选择">
          <el-option
            v-for="item in optionsGiveAwayType"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="row" v-if="inputGiveAway!=0">
        <span>赠送数量：</span>
        <el-input placeholder="请输入赠送数量" v-model="inputGiftAmount" clearable></el-input>
      </div>
      <div class="row">
        <span>价格：</span>
        <el-input placeholder="请输入金币数量" v-model="inputPrice" clearable type="number"></el-input>
      </div>
      <div class="row">
        <span>标题描述：</span>
        <el-input placeholder="请输入排序" v-model="inputTitleDesc" clearable type="textarea"></el-input>
      </div>
      <div class="row">
        <span>价格描述：</span>
        <el-input placeholder="请输入排序" v-model="inputPriceDesc" clearable type="textarea"></el-input>
      </div>
      <div class="row">
        <span>排序：</span>
        <el-input placeholder="请输入排序" v-model="inputSort" clearable type="number"></el-input>
      </div>
      <div class="row">
        <span>状态：</span>
        <el-select v-model="inputStatus" placeholder="请选择">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </div>
      <div class="dialogBtn">
        <el-button type="primary" size="small" @click="submitModifyEditor">提交</el-button>
        <el-button type="info" size="small" @click="cancelModifyEditor">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "goldPackageManagement",
  data() {
    return {
      options2: [
        {
          value: 0,
          label: "未启动"
        },
        {
          value: 1,
          label: "启动"
        }
      ],
      optionsGiveAwayType:[
        {
          value: 0,
          label: "无赠送"
        },
        {
          value: 1,
          label: "会员"
        },
        {
          value: 2,
          label: "钻石"
        },
      ],
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      dialogTitle: "",
      modifyEditorDialogVisible: false,
      //弹窗金额
      inputAmount: "",
      inputGiveAway:"",
      inputGiftAmount: 0,
      //弹窗金币
      inputPrice: "",
      inputTitleDesc:"",
      inputPriceDesc:"",
      //弹窗状态
      inputStatus: "",
      //弹窗排序
      inputSort: "",
      initData: {},
      selectId:"",
      // 权限管理
      permissions: [],
      permissions_obj: {}
    };
  },
  created() {
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff = false;
    this.permissions.forEach((item, index) => {
      if (item == -1) {
        oneOff = true;
      } else {
        this.permissions_obj["id_" + item] = item;
      }
    });
    if (oneOff) {
      this.permissions_obj.id_all = true;
    } else {
      this.permissions_obj.id_all = false;
    }
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getDataList();
    },
    //新增
    addData() {
      this.inputAmount="";
      this.inputGiveAway="";
      this.inputGiftAmount=0;
      this.inputPrice="";
      this.inputTitleDesc="";
      this.inputPriceDesc="";
      this.inputSort="";
      this.inputStatus="";
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "新建钻石套餐";
    },
    //编辑
    handleEdit(index, row) {
      this.selectId=row.id;
       this.inputAmount=row.coins;
       this.inputGiveAway=row.send_type;
      this.inputGiftAmount=row.send_num;
      this.inputPrice=row.price;
      this.inputTitleDesc=row.remark;
      this.inputPriceDesc=row.intro;
      this.inputSort=row.sort;
      this.inputStatus=row.status;
      this.modifyEditorDialogVisible = true;
      this.dialogTitle = "修改钻石套餐";
    },
    //提交修改或者新增
    submitModifyEditor() {
      if (this.dialogTitle == "新建钻石套餐") {
        this.addMethod();
      } else {
        this.modifyMethod();
      }
    },
    //取消提交
    cancelModifyEditor() {
      this.modifyEditorDialogVisible = false;
    },
    //获取初始化列表数据
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize
        }
      };
      let res = await this.$axios.post(
        this.$api.coinsList,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.initData = res.data;
        this.initData.data.forEach((item, index) => {
          if(item.status==0){
              item.status_text="未启动";
          }else if(item.status==1){
              item.status_text="已启动";
          };
          if(item.send_type==1){
              item.send_type_text="会员";
          }else if(item.send_type==2){
              item.send_type_text="钻石";
          }else if(item.send_type==0){
              item.send_type_text="无赠送";
          };
        });
        this.totalSize = this.initData.total;
        this.tableData = this.initData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //添加接口
    async addMethod() {
      let parames = {
        handshake: "",
        data: {
          coins:this.inputAmount,
          send_type:this.inputGiveAway,
          send_num: this.inputGiftAmount,
          price: this.inputPrice,
          remark:this.inputTitleDesc,
          intro:this.inputPriceDesc,
          sort:this.inputSort,
          status:this.inputStatus,
        }
      };
      let res = await this.$axios.post(
        this.$api.coinsAdd,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    //编辑接口
    async modifyMethod() {
      let parames = {
        handshake: "",
        data: {
          id: this.selectId,
          coins:this.inputAmount,
          send_type:this.inputGiveAway,
          send_num: this.inputGiftAmount,
          price: this.inputPrice,
          remark:this.inputTitleDesc,
          intro:this.inputPriceDesc,
          sort:this.inputSort,
          status:this.inputStatus,
        }
      };
      let res = await this.$axios.post(
        this.$api.coinsUpdate,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.modifyEditorDialogVisible = false;
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
</style>